package com.gongj.aop.JDKProxy;

import com.gongj.aop.GongService;
import com.gongj.aop.User;
import com.gongj.aop.YuanInterface;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class JDKProxy {

	public static void main(String[] args) {

		GongService gongService = new GongService(); //目标对象
		//ClassLoader loader,
		//Class<?>[] interfaces,
		//InvocationHandler h
		YuanInterface instance = (YuanInterface)Proxy.newProxyInstance(GongService.class.getClassLoader(), GongService.class.getInterfaces(), new InvocationHandler() {
			@Override
			public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
				System.out.println("执行 JDKProxy 代理逻辑之前");
				Object invoke = method.invoke(gongService, args);
				System.out.println("执行 JDKProxy 代理逻辑之后");
				return invoke;
			}
		});

		User jie = instance.getUserInfo("jie");
		System.out.println("代理对象 = " + instance);
	}
}
